import java.io.*;

/**
 * Created by foredawn on 15-8-28.
 * 过滤流:ObjectInputStream
 */
public class ObjectStream {

    public static void main(String[] args) {

        write();
        read();
    }

    public static void write() {
        try (ObjectOutputStream out = new ObjectOutputStream(
                new FileOutputStream(Canstant.FILE_NAME))) {
            User user = new User();
            user.setName("小明");
            user.setAge(18);
            user.setHobby("游泳");
            out.writeObject(user);//写入一个对象
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void read() {
        try (ObjectInputStream input = new ObjectInputStream(
                new FileInputStream(Canstant.FILE_NAME))) {
            User user = (User) input.readObject();
            System.out.println(user);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
